package edu.bu.cs673.superkids123.common.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import edu.bu.cs673.superkids123.HibernateUtil;
import edu.bu.cs673.superkids123.children.model.Level;

@Entity
@Table(name="eventlog")
public class EventLog {
	
	@Id @GeneratedValue
	@Column(name="EventLogID")
	private int id;
	
	@Column(name="EventType")
	private char type; //I , E , or W
	
	@Column(name="EventTime")
	private Date time;
	
	@Column(name="EventsDescription")
	private String description;
	
	@Column(name="EventSource")
	private String source;// Max 100 char
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public char getType() {
		return type;
	}
	public void setType(char type) {
		this.type = type;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getSource() {
		return source;
	}
	public void setSource(String source) {
		this.source = source;
	}
	
	
	public EventLog(){
		
	}
	public EventLog(char type, Date time, String description, String source)
	{
		this.type= type;
		this.time = time;
		this.description= description;
		this.source = source;
	}
	
	public static void logEvent(EventLog event)
	{
		Transaction tx = null;
		Session session = HibernateUtil.getSessionFactory().openSession();

		try {

			tx = session.beginTransaction();
			session.save(event);
			session.getTransaction().commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
